# md5加密
def md5_tool(str):
    import hashlib
    md = hashlib.md5()
    md.update(str.encode())
    res = md.hexdigest()
    return res


# print(md5_tool('dsfjkd'))


# 字典转json

def my_json(dict):
    import json
    return json.dumps(dict, ensure_ascii=False)


# # 链接数据库
#
# import pymysql
#
# # 连接
# try:
#     conn = pymysql.connect(host='47.94.144.144', user='appuser', password='appuser007', db='tiantian_v2', port=3306)
# except Exception as e:
#     print('数据库连接出错,报错信息:%s',e)
# finally:
#     # 游标
#     cursor = conn.cursor()
#
# # SQL语句
# # 查询用户信息SQL
# selectUserInfoSql = "select user_id, user_mobile, user_real_name, user_idcard_auth_status, user_info_auth_status, user_mobile_auth_status, user_zhima_auth_status,user_taobao_auth_status, user_jingdong_auth_status,user_credit_card_auth_status, user_shebao_auth_status, user_gongjijin_auth_status, user_salary_card_auth_status, user_credit_money, user_available_money, user_credit_money_expire, user_credit_status from tiantian_v2.tt_user_index where user_id = 144;"
# # 修改用户信用额度SQL
# updateCreditSql = 'update tiantian_v2.tt_user_index set user_credit_money=10000, user_available_money=10000 where user_id=144;'
# # 修改用户认证状态SQL
# updateUserStatusSql = 'update tiantian_v2.tt_user_index set user_idcard_auth_status=1, user_info_auth_status=1, user_mobile_auth_status=1, user_zhima_auth_status=1,user_taobao_auth_status=1, user_jingdong_auth_status=1,user_credit_card_auth_status=1, user_shebao_auth_status=1, user_gongjijin_auth_status=1, user_salary_card_auth_status=1 where user_id = 144;'
# # 执行SQL
# try:
#     cursor.execute(selectUserInfoSql)
#     cursor.execute(updateCreditSql)
#     cursor.execute(selectUserInfoSql)
# except Exception as e:
#     print('执行sql语句报错,报错信息:%s',e)
# finally:
#     # 获取查询的所有记录
#     result = cursor.fetchall()
#     print(result)
#
# # 关闭游标关闭连接
# cursor.close()
# conn.close()

class conMysql(object):
    def __init__(self, host, user, password, db, port):
        self.host = host
        self.user = user
        self.password = password
        self.db = db
        self.port = port
        self.__conn()

    def __conn(self):
        import pymysql
        try:
            self.conn = pymysql.connect(self.host, self.user, self.password, self.db, self.port)
        except Exception as e:
            print('数据库连接出错:%s', e)
        finally:
            self.cursor = self.conn.cursor()

    def executeSelectSql(self, sql):
        try:
            self.cursor.execute(sql)
        except Exception as e:
            print('执行SQL语句出错:%s', e)
        finally:
            return self.cursor.fetchall()

    def executeOtherSql(self, sql):
        try:
            self.cursor.execute(sql)
        except Exception as e:
            print('执行SQL语句出错:%s', e)
        finally:
            self.conn.commit()

    def __del__(self):
        self.cursor.close()
        self.conn.close()


# # 示例
# con = conMysql('47.94.144.144', 'appuser', 'appuser007', 'tiantian_v2', 3306)
#
# result1 = con.executeSelectSql(selectUserInfoSql)
# con.executeOtherSql(updateUserStatusSql)
# result3 = con.executeSelectSql(selectUserInfoSql)
# print(result1, result3)



# 13.10.3 风控机审完成后获取额度，更新审核结果
# import requests
#
# data = {
#     'biz': '3642',
#     'creditStatus': '3',
#     'machineCreditMoney': '5000'
#     'caseId':'5000'
# }
# url = 'http://apiv2.tt.sanwenqian.cn/api/user/updateUserMachineCreditMoney'
# res = requests.post(url, data)
# print(res.json())
